<?php
include_once("../configure/admin.config.inc.php");	//--> admin global var
include_once("db.inc.php");							//--> db global var
include_once("Smarty.class.php");					//--> out template
include_once("Operation.php");						//--> Authority
require_once("controlHeader.php");					//--> system control header
include_once("ShippingFee.php");					//--> ShippingFee


$objOperate		= new Operation($objSession->getLanguage());	//--> Operation
$objShippingFee	= new ShippingFee(DB_TAG_SYSTEM, $uid);			//--> ShippingFee

$countryid = 0;


$arrOperate = $objOperate->arrGetFromGroupIDAndMenuID($gid, $menuid);

if(!empty($id)){
	$arrData 			= $objShippingFee->getByID($arrOperate, $id);
 	
 	$zoneid 			= $arrData['zoneID'];
	$shippingMethodID 	= $arrData['shippingMethodID'];
	$shippingTypeID 	= $arrData['shippingTypeID'];
	$amount 			= $arrData['amount'] / 100;
	$amount2 			= $arrData['amount2'] / 100;
	$summary 			= $arrData['summary'];
	
	$countryid = $objShippingFee->getColumn(DB_TAG_PUBLIC, "SELECT countryID FROM zones WHERE id = $zoneid");
}

if(isset($_POST['countryid'])){
	$countryid = $_POST['countryid'];
}

if(isset($_POST['Submit'])){
	$zoneid 			= $_POST['zoneid'];
	$shippingMethodID 	= $_POST['shippingMethodID'];
	$shippingTypeID 	= $_POST['shippingTypeID'];
	$amount 			= $_POST['amount'];
	$amount2 			= $_POST['amount2'];
	$summary 			= $_POST['summary'];
	
	if(empty($summary)){
		$summary = $objShippingFee->getColumn(DB_TAG_PUBLIC, "SELECT code FROM zones WHERE id = $zoneid");
	}
	
	if($amount == "")
		$error_message = 'Amount should\'t be empty.';
	else if($amount2 == "")
		$error_message = 'Amount2 should\'t be empty.';
	else{
		$isReturn = $objShippingFee->edit($arrOperate, $id, $zoneid, $shippingMethodID, $shippingTypeID, $amount * 100, $amount2 * 100, $summary, $status = 'yes');
		if($isReturn)
			$error_message = 'edit successfully.';
		else 
			$error_message = 'edit failure.';
	}
}

$arrCountry	= $objShippingFee->getHash(DB_TAG_PUBLIC, "SELECT id, name FROM countries WHERE id in (SELECT DISTINCT countryID FROM zones)");

if(empty($countryid)){
	foreach($arrCountry as $key => $value){
		$countryid = $key;
		
		break;
	}
}


if(!empty($countryid))
	$arrZones = $objShippingFee->getHash(DB_TAG_PUBLIC, "SELECT id, name FROM zones WHERE countryID = $countryid");


$arrShippingType 	= $objShippingFee->getHash(DB_TAG_PUBLIC, "SELECT id, name FROM shipping_type");
$arrShippingMethod 	= $objShippingFee->getHash(DB_TAG_PUBLIC, "SELECT id, name FROM shipping_method");


/*----- out html -----*/
$smarty = new Smarty();		//----- out template
$smarty->template_dir = TEMPLATE_SYS_DIR;
$smarty->compile_dir  = COMPILE_SYS_DIR;
$smarty->assign('menuGid', $menuGid);
$smarty->assign('menuid', $menuid);
$smarty->assign('id', $id);

$smarty->assign('backurl',$backurl);
$smarty->assign('error_message', $error_message);

$smarty->assign('arrZones', $arrZones);
$smarty->assign('zoneid', $zoneid);

$smarty->assign('arrCountry', $arrCountry);
$smarty->assign('countryid', $countryid);

$smarty->assign('arrShippingType', $arrShippingType);
$smarty->assign('shippingTypeID', $shippingTypeID);

$smarty->assign('arrShippingMethod', $arrShippingMethod);
$smarty->assign('shippingMethodID', $shippingMethodID);

$smarty->assign('amount', $amount);
$smarty->assign('amount2', $amount2);


$smarty->assign('summary', $summary);

$smarty->display('listShippingFee_Edit.htm');
?>
